function GetQueryString(name)
{
     var reg = new RegExp("(^|&)"+ name +"=([^&]*)(&|$)");
     var r = window.location.search.substr(1).match(reg);
     if(r!=null)return  unescape(r[2]); return null;
}

$(document).ready(function(){
	$.ajax({
            url:'/hotFixServer/appversion/appVersionList?applicationId='+GetQueryString("id")+'&page=1&size=100',
            type:"GET",
			cache:false,
            success:function(data){
                for(var i=0;i<data.model['appVersionList'].length;i++){
				var versionTable="<tr id="+data.model['appVersionList'][i]['id']+">\r\n"+
							"<td>"+data.model['appVersionList'][i]['app_version_name']+"</td>\r\n"+
							"<td>"+data.model['appVersionList'][i]['app_version_code']+"</td>\r\n"+
							"<td>"+data.model['appVersionList'][i]['create_time'].split("T")[0]+"</td>\r\n"+
							"<td><a href=\"managepatch.html?id="+data.model['appVersionList'][i]['id']+"&applicationId="+GetQueryString("id")+"\" style=\"color:#CD8162\">查看详情</a>|<a  style=\"color:#242424\" href='javascript:void(0)' onclick=\"deleteAppVersion('"+data.model['appVersionList'][i]['id']+"')\">删除</a></td>\r\n"+
							"</tr>";
				$("#versionTable").append(versionTable);
			}
		}
    });
	$.get('/hotFixServer/application/getAppInfo?appId='+GetQueryString("id"),function(data){
		var appType="android";
		if(data.model['appInfo']['app_type']==1){
			appType="ios";
		}
		$("#app").text(data.model['appInfo']['app_name']);
		$("#appName").text(data.model['appInfo']['app_name']);
		$("#packageName").text(data.model['appInfo']['bundle_id']);
		$("#appType").text(appType);
		$("#creatTime").text(data.model['appInfo']['create_time'].split("T")[0]);
		$("#description").text(data.model['appInfo']['description']);
		$("#appid").text(data.model['appInfo']['id']);
		
	});
	

});

function creatAppVersion(){
	var version=$("#versionNum").val();
	var versionCode=$("#versionCode").val();
	if(version==""){
		layer.alert('版本号不能为空', {
									icon: 5,
									skin: 'layer-ext-moon'
						});
	}
	else if(versionCode==""){
		layer.alert('版本号code不能为空', {
									icon: 5,
									skin: 'layer-ext-moon'
						});
	}
	else{
		var postData={
		"appVersionName":version,
		"appVersionCode":versionCode,
		"appId":GetQueryString("id")
		}
		$.ajax({
				type:"POST",
				url:"/hotFixServer/appversion/createAppVersion",
				data:postData,
				success: function(data){
					if(data.model['success']==0){
						layer.alert('创建成功', {
										icon: 6,
										skin: 'layer-ext-moon'
							});
						$("#versionNum").val("");
						$("#versionCode").val("");
						$("#myVersionModal").modal('hide');
						var appVersionListHtmlAdd="<tr id="+data.model['appVersionId']+">\r\n"+
								"<td>"+version+"</td>\r\n"+
								"<td>"+versionCode+"</td>\r\n"+
								"<td>"+data.model['createTime'].split(" ")[0]+"</td>\r\n"+
								"<td><a href=\"managepatch.html?id="+data.model['appVersionId']+"&applicationId="+GetQueryString("id")+"\" style=\"color:#CD8162\">查看详情</a>|<a  style=\"color:#242424\" href='javascript:void(0)' onclick=\"deleteAppVersion('"+data.model['appVersionId']+"')\">删除</a></td>\r\n"+
								"</tr>";
						$("#versionTable").prepend(appVersionListHtmlAdd);
					}
					else if(data.model['success']==1){
						layer.alert(data.model['info'], {
										icon: 5,
										skin: 'layer-ext-moon'
							});
					}
					else if(data.model['success']==-1){
						layer.alert(data.model['info'], {
										icon: 5,
										skin: 'layer-ext-moon'
							});
					}
					else if(data.model['success']==2){
						layer.alert(data.model['info'], {
										icon: 5,
										skin: 'layer-ext-moon'
							});
					}
					
				}
			});
	}
	
	
}

//删除app版本
function deleteAppVersion(id){
	var deleteData={
		"appVersionId":id
		
	}
    layer.confirm('您确定删除吗？', {
        btn: ['确定','取消'] //按钮
    }, function(){
        $.ajax({
            url:'/hotFixServer/appversion/deleteAppVersion',
            type:"DELETE",
            data:deleteData,
            success:function(data){
                if(data.model['success']==0){
                    layer.alert(data.model['info'], {
                        icon: 6,
                        skin: 'layer-ext-moon'
                    });
                    $('#'+id).remove();
                }
                else if(data.model['success']==1){
                    layer.alert(data.model['info'], {
                        icon: 5,
                        skin: 'layer-ext-moon'
                    });
                    location.reload(false);
                }
                else if(data.model['success']==-1){
                    layer.alert(data.model['info'], {
                        icon: 5,
                        skin: 'layer-ext-moon'
                    });
                    location.reload(false);
                }
            },
            error:function(xhr,textstatus,thrown){

            }
        });
    }, function(){
        layer.closeAll();
    });

}

//返回上一页
function back(){
	history.go(-1);
}